package com.hy.service.impl;

import com.hy.bean.Clienttable;
import com.hy.bean.RespBean;
import com.hy.dao.ClienttableDao;
import com.hy.dao.impl.ClienttableDaoImpl;
import com.hy.service.ClienttableService;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class ClienttableServiceImpl implements ClienttableService {
    //TODO
    private ClienttableDao clienttableDao = new ClienttableDaoImpl();

    @Override
    public RespBean getPrintClientLimit(Integer pages, Integer currentCount, String clientname, String linkman, String type) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("totao",clienttableDao.selectCount(clientname,linkman,type));
        map.put("clienttableList",clienttableDao.selectPrintClientLimit(pages,currentCount,clientname,linkman,type));
        return RespBean.success("ok",map);
    }

    @Override
    public RespBean queryByClientName(String clientName) {
        Clienttable clienttable = clienttableDao.queryByClientName(clientName);

        if (clienttable == null)
            return RespBean.success("该客户可以添加");

        return RespBean.success("该客户已存在");
    }

    @Override
    public RespBean queryAll(Integer page,Integer limit) {
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("data",clienttableDao.queryAll(page,limit));
        hashMap.put("total",clienttableDao.count());
        return RespBean.success("ok",hashMap);
    }

    @Override
    public RespBean addClienttable(Clienttable clienttable){
        int count = clienttableDao.insert(clienttable);

        if (count > 0)
            return RespBean.success("添加成功");

        return RespBean.success("添加失败");
    }

    @Override
    public RespBean getById(Integer id) {
        Clienttable clienttable = clienttableDao.queryById(id);
        if (clienttable == null){
            return RespBean.success("服务器异常，请稍后重试");
        }
        return RespBean.success("ok",clienttable);
    }

    @Override
    public RespBean updateClienttable(Clienttable clienttable) {
        int count = clienttableDao.update(clienttable);
        if (count > 0){
            return RespBean.success("修改成功");
        }
        return RespBean.success("修改失败");
    }

    @Override
    public RespBean deleteById(Integer id) {
        int delete = clienttableDao.deleteById(id);

        if (delete > 0)
            return RespBean.success("删除成功");

        return RespBean.success("删除失败");
    }
}